package com.gqj.demo;

import com.gg.reader.api.dal.GClient;
import com.gg.reader.api.dal.HandlerTagEpcLog;
import com.gg.reader.api.dal.HandlerTagEpcOver;
import com.gg.reader.api.protocol.gx.*;

import java.util.Scanner;

public class start {

	public static void start16() {
		 GClient client = new GClient();
	     Scanner sc = new Scanner(System.in);
	     if (client.openTcp("192.168.1.168:8160", 0)) {
	            // 订阅标签上报事件
	            client.onTagEpcLog = new HandlerTagEpcLog() {
	                @Override
	                public void log(String readName, LogBaseEpcInfo logBaseEpcInfo) {
	                    // 回调内部如有阻塞，会影响API正常使用
	                    // 标签回调数量较多，请将标签数据先缓存起来再作业务处理
	                    if (null != logBaseEpcInfo && 0 == logBaseEpcInfo.getResult()) {
	                        System.out.println(logBaseEpcInfo.getEpc());
	                    }
	                }
	            };
	            client.onTagEpcOver = new HandlerTagEpcOver() {
	                @Override
	                public void log(String readName, LogBaseEpcOver logBaseEpcOver) {
	                    if (null != logBaseEpcOver) {
	                        System.out.println("Epc log over.");
	                    }
	                }
	            };
            // 停止指令，空闲态
            MsgBaseStop msgBaseStop = new MsgBaseStop();
            client.sendSynMsg(msgBaseStop);
            if (0 == msgBaseStop.getRtCode()) {
                System.out.println("Stop successful.");
            } else {
                System.out.println("Stop error.");
            }

            // 4个天线读卡, 读取EPC数据区以及TID数据区
            MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc();
            msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1 | EnumG.AntennaNo_2 | EnumG.AntennaNo_3 | EnumG.AntennaNo_4);
            msgBaseInventoryEpc.setInventoryMode(EnumG.InventoryMode_Inventory);

            //ParamEpcReadTid tid = new ParamEpcReadTid();
            //tid.setMode(EnumG.ParamTidMode_Auto);
            //tid.setLen(6);
            //msgBaseInventoryEpc.setReadTid(tid);
            MsgAppSetGpo msgAppSetGpo = new MsgAppSetGpo();
            msgAppSetGpo.setGpo1(1);

            client.sendSynMsg(msgBaseInventoryEpc);
            if (0 == msgBaseInventoryEpc.getRtCode()) {
                System.out.println("Inventory epc successful.");
            } else {
                System.out.println("Inventory epc error.");
            }

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 停止读卡，空闲态
            client.sendSynMsg(msgBaseStop);
            if (0 == msgBaseStop.getRtCode()) {
                System.out.println("Stop successful.");

                client.close();
            } else {
                System.out.println("Stop error.");
            }

            //System.out.println("Close the connection");
            // client.close();

        } else {
            System.out.println("Connect failure.");
        }
    }

}
